﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ConversionTools;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.SpatialAnalystTools;
using ESRI.ArcGIS.GeoAnalyst;
using ESRI.ArcGIS.ADF.Connection.Local;

namespace SPAS
{
    #region 柳伟佳编写

    [Serializable]
    class ShadowSimulation
    {
        private ISurfaceOp2 _surfaceOp; // 表明分析对象 
        private IGeoDataset _demRaster; // DEM栅格数据
        private List<IGeoDataset> _outputRasters = new List<IGeoDataset>(); // 输出栅格列表
        private DateTime _startTime;
        private DateTime _endTime;
        private double _deltaTime;
        private double _latitude;
        private double _longitude;

        public int RasterCount { get { return _outputRasters.Count; } }
        public List<IGeoDataset> OutputRasters { get { return _outputRasters; } }

        public ShadowSimulation()
        {

        }

        /// <summary>
        /// 构造函数 delta (min)
        /// </summary>
        /// <param name="raster"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="delta"></param>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        public ShadowSimulation(IRaster raster, DateTime start, DateTime end, double delta, double latitude, double longitude)
        {
            _demRaster = raster as IGeoDataset;
            _startTime = start;
            _endTime = end;
            _deltaTime = delta;
            _latitude = latitude;
            _longitude = longitude;
        }

        /// <summary>
        /// 进行模拟
        /// </summary>
        public void Simulate()
        {
            _surfaceOp = new RasterSurfaceOpClass();
            var during = _endTime.TimeOfDay - _startTime.TimeOfDay;
            DateTime time = _startTime;
            while (time <= _endTime)
            {
                double altitude, azimuth;
                Utils.ComputeAltitudeAndAzimuth(time, _latitude, out altitude, out azimuth);
                if (altitude > 0 && altitude < 90)
                    _outputRasters.Add(_surfaceOp.HillShade(_demRaster, azimuth, altitude, true));
                time = time.AddMinutes(_deltaTime);
            }
        }
    }
    #endregion
}
